package com.anhry.www.safety.manage.enterprise.struts.action;

import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.www.safety.manage.enterprise.bean.TbCorpPerson;
import com.anhry.www.safety.manage.enterprise.bean.vo.CorpBean;
import com.anhry.www.safety.manage.enterprise.struts.base.EnterpriseBaseAction;
import com.anhry.www.safety.util.Loggers;
import com.anhry.www.safety.util.PwdDigest;
import com.anhry.www.safety.util.Util;
import com.opensymphony.xwork2.ModelDriven;

/**
 * corpRenzhengxinx
 * @author chen
 * @date 2012-9-10
 */

@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class CorpInfoAction extends EnterpriseBaseAction implements ModelDriven<CorpBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private CorpBean corpBean = new CorpBean(); // 模型驱动

	private String corpId;
	private String perId;
	private TbCorpPerson per;
	private TbCorpInfo corp;
	private TbCorpInfo corpEdit;
	private String oldPwd;
	private String newPwd;
	private String perName;
	private String perSex;
	private Date perBirthday;
	private String perPhone;
	private String perMobile;
	private String perEmail;
	private String perCode;
	private Date certBtime;
	private Date certEtime;
	private String perType;
	private String position;
	private String perDegree;//学历
	private String perTraining;//培训情况
	private String perWorkingYear;//工作年限
	private String note; 

	/**
	 * 企业基础信息
	 * @return
	 */
	public String lookInfo() {
		try {
			TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			this.corp = (TbCorpInfo) enterprisService.getCorpById(sessioncorp.getId());
			TbCorpPerson safety = enterprisService.getCorpSafetyHead(corp.getId(), "3");// 获取安全负责人
			if (safety != null) {
				this.corp.setSafetyHead(safety.getPerName());
				this.corp.setSafetyHeadTel(safety.getPerPhone());
				this.corp.setSafetyHeadMobile(safety.getPerMobile());
				this.corp.setSafetyHeadDegree(safety.getPerDegree());
				this.corp.setSafetyHeadTraining(safety.getPerTraining());
				this.corp.setSafetyHeadWorkingYear(safety.getPerWorkingYear());
			}
			TbCorpPerson mainHead = enterprisService.getCorpSafetyHead(corp.getId(), "2");// 获取主要负责人
			if (mainHead != null) {
				this.corp.setMainHead(mainHead.getPerName());
				this.corp.setMainHeadTel(mainHead.getPerPhone());
				this.corp.setMainHeadMobile(mainHead.getPerMobile());
				this.corp.setMainHeadDegree(mainHead.getPerDegree());
				this.corp.setMainHeadTraining(mainHead.getPerTraining());
				this.corp.setMainHeadWorkingYear(mainHead.getPerWorkingYear());
			}
			this.setResult("corp_look");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpInfoAction:lookInfo() 异常：" + e.getMessage());
		}
		return ERROR;
	}

	/**
	 * 企业人员列表
	 * @return
	 */
	public String perList() {
		try {
			TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			if (sessioncorp != null) {
				this.setObjList((List) this.utilService.getHqlQuery("from TbCorpPerson t where 1=1 and t.corpId="+sessioncorp.getId()+" order by t.perType,t.id"));
				this.setResult("corp_per_list");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpInfoAction:preList() 异常："+ e.getMessage());
		}
		return ERROR;
	}

	/**
	 * 跳转到企业修改页面
	 * @return
	 */
	public String edit() {
		try {

			TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			this.corp = (TbCorpInfo) enterprisService.getCorpById(sessioncorp.getId());
			this.setResult("corp_edit");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpInfoAction:edit() 异常："+ e.getMessage());
		}
		return ERROR;
	}

	/**
	 * 企业信息保存
	 * @return
	 */
	public String save() {
		try {

			TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
			this.corp = (TbCorpInfo) enterprisService.getCorpById(sessioncorp.getId());
			this.corp.setCorpJingyixiangmu(corpEdit.getCorpJingyixiangmu());
			this.corp.setCorpYuanliao(corpEdit.getCorpYuanliao());
			this.corp.setCorpChanpin(corpEdit.getCorpChanpin());
			this.corp.setCorpIsDanger(corpEdit.getCorpIsDanger());
			this.corp.setCorpDanger(corpEdit.getCorpDanger());
			this.corp.setCorpIsShewei(corpEdit.getCorpIsShewei());
			this.corp.setCorpShewei(corpEdit.getCorpShewei());
			this.corp.setCorpIsTeshe(corpEdit.getCorpIsTeshe());
			this.corp.setCorpTeshe(corpEdit.getCorpTeshe());
			this.corp.setCorpIsZyb(corpEdit.getCorpIsZyb());
			this.corp.setCorpZyb(corpEdit.getCorpZyb());
			this.corp.setCorpXkEnd(corpEdit.getCorpXkEnd());
			this.corp.setCorpXkStart(corpEdit.getCorpXkStart());
			this.corp.setCorpXkCode(corpEdit.getCorpXkCode());
			this.corp.setCorpAqrs(corpEdit.getCorpAqrs());
			this.corp.setCorpCyrs(corpEdit.getCorpCyrs());
			this.corp.setCorpDixiayongtu(corpEdit.getCorpDixiayongtu());
			this.corp.setCorpDixia(corpEdit.getCorpDixia());
			this.corp.setCorpDishang(corpEdit.getCorpDishang());
			this.corp.setCorpPost(corpEdit.getCorpPost());
			this.corp.setCorpPhone(corpEdit.getCorpPhone());
			this.corp.setCorpEmail(corpEdit.getCorpEmail());
			this.corp.setCorpOfficeAddress(corpEdit.getCorpOfficeAddress());
			this.corp.setCorpWorkAddress(corpEdit.getCorpWorkAddress());
			this.corp.setIsBlastingDemolition(corpEdit.getIsBlastingDemolition());
			this.corp.setNationGrade(corpEdit.getNationGrade());
			this.corp.setCorpStatus(corpEdit.getCorpStatus());
			this.corp.setCorpZhucezichang(corpEdit.getCorpZhucezichang());
			this.corp.setCorpGudingzichang(corpEdit.getCorpGudingzichang());
			this.corp.setUpdateTime(new Date());
			this.corp.setUpdateFalg("2");
			this.corp.setUpdateUser(this.corp.getCorpName());
			this.corp.setCorpRenzhengxinxi(corpEdit.getCorpRenzhengxinxi());
			this.corp.setCorpWhxkCode(corpEdit.getCorpWhxkCode());//危化认可起证
			this.corp.setCorpWhxkStart(corpEdit.getCorpWhxkStart());//危化认可起始时间
			this.corp.setCorpWhxkEnd(corpEdit.getCorpWhxkEnd());//危化认可结束时间
			this.corp.setCorpMobilePhone(corpEdit.getCorpMobilePhone());//企业联系手机
			this.corp.setReportType(corpEdit.getReportType());//自报类型
			this.utilService.update(corp);
			this.setTip("企业信息修改成功！");
			return this.edit();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpInfoAction:corpInfoEdit() 异常："+ e.getMessage());
		}
		return ERROR;
	}

	/**
	 * 进入修改密码页面
	 * 
	 * @return
	 */
	public String toUpdatePwd() {
		this.setResult("updatePwd");
		return SUCCESS;
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 */
	public String updatePwd() {
		try {
			TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);

			if (sessioncorp != null) {
				if (oldPwd != null && !"".equals(oldPwd) && newPwd != null && !"".equals(newPwd)) {
					String str = PwdDigest.passwordDigest(oldPwd);
					if (str.equals(sessioncorp.getCorpPwd())) {
						// 旧密码无误
						TbCorpInfo corp = (TbCorpInfo) this.utilService.getObjectById(TbCorpInfo.class, sessioncorp.getId());
						corp.setCorpPwd(PwdDigest.passwordDigest(newPwd));
						corp.setCorpPwd2(newPwd);
						this.utilService.update(corp);
						this.setTip("密码修改成功！");
						return "index";
					} else {
						this.setTip("原密码有误，请重新输入！");
						this.setResult("updatePwd");
						return SUCCESS;
					}
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpInfoAction:preList() 异常："+ e.getMessage());
		}
		return ERROR;
	}

	// 查看 人员 信息
	public String toLookPer() {
		try {
			if (perId != null && !"".equals(perId)) {
				this.per = (TbCorpPerson) this.getUtilService().getObjectById(TbCorpPerson.class, Long.parseLong(perId));
				this.setResult("corp_lookPer");
				//tip = null;
				return SUCCESS;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpInfoAction:toLookPer() 异常："
					+ e.getMessage());
		}
		return ERROR;
	}

	/**
	 * 跳转到人员添加页面
	 * @return
	 */
	public String toAddPer() {
		this.setResult("corp_per_add");
		return SUCCESS;
	}

	/**
	 * 添加人员
	 * @return
	 */
	public String addPer() {
		TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
		if (null != sessioncorp && sessioncorp.getId() != null) {
			try {
				this.corpId = sessioncorp.getId().toString();
				TbCorpPerson per = new TbCorpPerson();
				per.setPerName(this.perName);
				per.setCorpId(sessioncorp.getId());
				per.setPerCode(perCode);
				per.setPerEmail(perEmail);
				per.setPerMobile(perMobile);
				per.setPerPhone(perPhone);
				per.setPerSex(perSex);
				per.setPerDegree(perDegree);
				per.setPerTraining(perTraining);
				per.setPerWorkingYear(perWorkingYear);
				per.setPerType(perType);
				per.setPosition(position);
				per.setNote(note);
				this.utilService.save(per);
			} catch (Exception e) {
				e.printStackTrace();
				Loggers.error("enterprise: CorpInfoAction:addPer() 异常："
						+ e.getMessage());
			}
			this.setTip("添加成功！");
			return this.perList();
		}

		return ERROR;
	}

	/**
	 * 跳转到人员修改页面
	 * @return
	 */
	public String toEditPer() {
		try {
			if (perId != null && !"".equals(perId)) {
				this.per = (TbCorpPerson) utilService.getObjectById(TbCorpPerson.class, Long.parseLong(perId));
				this.setResult("corp_per_edit");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpInfoAction:toEditPer() 异常："+ e.getMessage());
		}
		return ERROR;
	}

	public String editPer() {
		TbCorpInfo sessioncorp = (TbCorpInfo) contextPvd.getSessionAttr(Util.SESSION_CORPUSER);
		try {
			if (null != sessioncorp && sessioncorp.getId() != null) {
				this.per = (TbCorpPerson) utilService.getObjectById(TbCorpPerson.class, Long.parseLong(perId));
				per.setPerName(this.perName);
				per.setPerEmail(this.perEmail);
				per.setPerMobile(this.perMobile);
				per.setPerPhone(this.perPhone);
				per.setPerSex(this.perSex);
				per.setPerDegree(this.perDegree);
				per.setPerTraining(this.perTraining);
				per.setPerWorkingYear(this.perWorkingYear);
				per.setNote(note);
				this.utilService.update(per);
				this.setTip("修改成功！");
				return this.perList();
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("enterprise: CorpInfoAction:editPer() 异常："+ e.getMessage());
		}

		return ERROR;
	}

	public String delPer() {
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		try {
			if (null != this.perId && this.perId.length() > 0) {
				TbCorpPerson delper = (TbCorpPerson) this.utilService.getObjectById(TbCorpPerson.class, Long.parseLong(this.perId));
				if(null!=delper){
					this.utilService.delete(delper);
					this.setTip("删除成功！");
					return this.perList();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("manager:CorpInfoAction:delPer()方法出错："+ e.getMessage());
		}
		return ERROR;
	}

	/*
	 * 密码重置
	 */
	public void resetPwd() {
		this.contextPvd.getResponse().setHeader("Cache-Control", "no-cache");
		try {
			if (null != this.corpId && this.corpId.length() > 0) {
				TbCorpInfo corp = (TbCorpInfo) this.utilService.getObjectById(TbCorpInfo.class, Long.parseLong(this.corpId));
				if (corp != null) {
					corp.setCorpPwd(PwdDigest.passwordDigest("666666"));
					corp.setCorpPwd2("666666");
					this.utilService.update(corp);
					this.contextPvd.getResponse().getWriter().write("1");
				} else {
					this.contextPvd.getResponse().getWriter().write("0");
				}
			} else {
				this.contextPvd.getResponse().getWriter().write("0");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			Loggers.error("manager:CorpInfoAction:resetPwd()方法出错：" + e);
			e.printStackTrace();
		}
	}

	/**
	 * 检测企业工商代码是否重复
	 */
	public void gsCodeCheck() {
		this.contextPvd.getResponse().setHeader("Cache-Control", "no-cache");
		try {
			String count = this.enterprisService.getGsCode(this.corpId,this.corpBean.getCorpGsCode());
			this.contextPvd.getResponse().getWriter().write(count);
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			Loggers.error("manager:CorpInfoAction:gsCodeCheck()方法出错：" + e);
			e.printStackTrace();
		}
	}

	/**
	 * 检查组织机构代码是否重复
	 * 
	 * @return
	 */
	public void zzCodeCheck() {
		this.contextPvd.getResponse().setHeader("Cache-Control", "no-cache");
		try {
			String count = this.enterprisService.getZzCode(this.corpId,this.corpBean.getCorpOrgCode());
			this.contextPvd.getResponse().getWriter().write(count);
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			Loggers.error("manager:CorpInfoAction:zzCodeCheck()方法出错：");
			e.printStackTrace();
		}
	}

	public CorpBean getCorpBean() {
		return corpBean;
	}

	public void setCorpBean(CorpBean corpBean) {
		this.corpBean = corpBean;
	}

	public CorpBean getModel() {
		return this.corpBean;
	}

	public String getCorpId() {
		return corpId;
	}

	public void setCorpId(String corpId) {
		this.corpId = corpId;
	}

	public TbCorpInfo getCorp() {
		return corp;
	}

	public void setCorp(TbCorpInfo corp) {
		this.corp = corp;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public TbCorpPerson getPer() {
		return per;
	}

	public void setPer(TbCorpPerson per) {
		this.per = per;
	}

	public String getPerId() {
		return perId;
	}

	public void setPerId(String perId) {
		this.perId = perId;
	}

	public Date getCertBtime() {
		return certBtime;
	}

	public void setCertBtime(Date certBtime) {
		this.certBtime = certBtime;
	}

	public Date getCertEtime() {
		return certEtime;
	}

	public void setCertEtime(Date certEtime) {
		this.certEtime = certEtime;
	}

	public Date getPerBirthday() {
		return perBirthday;
	}

	public void setPerBirthday(Date perBirthday) {
		this.perBirthday = perBirthday;
	}

	public String getPerCode() {
		return perCode;
	}

	public void setPerCode(String perCode) {
		this.perCode = perCode;
	}

	public String getPerEmail() {
		return perEmail;
	}

	public void setPerEmail(String perEmail) {
		this.perEmail = perEmail;
	}

	public String getPerMobile() {
		return perMobile;
	}

	public void setPerMobile(String perMobile) {
		this.perMobile = perMobile;
	}

	public String getPerName() {
		return perName;
	}

	public void setPerName(String perName) {
		this.perName = perName;
	}

	public String getPerPhone() {
		return perPhone;
	}

	public void setPerPhone(String perPhone) {
		this.perPhone = perPhone;
	}

	public String getPerSex() {
		return perSex;
	}

	public void setPerSex(String perSex) {
		this.perSex = perSex;
	}

	public String getPerType() {
		return perType;
	}

	public void setPerType(String perType) {
		this.perType = perType;
	}

	public TbCorpInfo getCorpEdit() {
		return corpEdit;
	}

	public void setCorpEdit(TbCorpInfo corpEdit) {
		this.corpEdit = corpEdit;
	}

	public String getPerDegree() {
		return perDegree;
	}

	public void setPerDegree(String perDegree) {
		this.perDegree = perDegree;
	}

	public String getPerTraining() {
		return perTraining;
	}

	public void setPerTraining(String perTraining) {
		this.perTraining = perTraining;
	}

	public String getPerWorkingYear() {
		return perWorkingYear;
	}

	public void setPerWorkingYear(String perWorkingYear) {
		this.perWorkingYear = perWorkingYear;
	}
	
	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

}
